package com.framework.service.module.job.impl;

import com.framework.common.model.response.R;
import com.framework.common.model.validation.ValidationGroup;
import com.framework.common.util.other.NumeralUtil;
import com.framework.mapper.module.job.ScheduledTasksLogMapper;
import com.framework.model.module.job.vo.ScheduledTasksLogVo;
import com.framework.model.system.vo.SystemUserVo;
import com.framework.service.base.BaseService;
import com.framework.service.module.job.ScheduledTasksLogService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.module.job.impl.ScheduledTasksLogServiceImpl
 * @description 定时任务日志业务接口实现类
 * @datetime 2025-04-25 09:06:26
 */
@Service("scheduledTasksLogServiceImpl")
public class ScheduledTasksLogServiceImpl extends BaseService implements ScheduledTasksLogService {
    private Logger log = LoggerFactory.getLogger(ScheduledTasksLogServiceImpl.class);
    @Autowired
    private ScheduledTasksLogMapper scheduledTasksLogMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.module.job.vo.ScheduledTasksLogVo
     * @title 根据编号查询定时任务日志
     * @description 根据编号查询定时任务日志
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    public ScheduledTasksLogVo getByIdParam(Long id) {
        ScheduledTasksLogVo row = new ScheduledTasksLogVo();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        ScheduledTasksLogVo p = scheduledTasksLogMapper.selectByParam(row);
        if (p == null) {
            return new ScheduledTasksLogVo();
        }
        return p;
    }

    /**
     * @param row 1 定时任务日志类
     * @return java.util.List<com.framework.model.module.job.vo.ScheduledTasksLogVo>
     * @title 查询定时任务日志集合
     * @description 查询定时任务日志集合
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    public List<ScheduledTasksLogVo> findByList(ScheduledTasksLogVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        List<ScheduledTasksLogVo> list = scheduledTasksLogMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 定时任务日志类
     * @return com.framework.common.model.response.R
     * @title 分页查询定时任务日志列表
     * @description 分页查询定时任务日志列表
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(ScheduledTasksLogVo row) {
//        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
//        Validator validator = factory.getValidator();
//        Set<ConstraintViolation<ScheduledTasksLogVo>> message = validator.validate(row, ValidationGroup.formEdit.class);
//        if (message.size() > NumeralUtil.POSITIVE_ZERO) {
//            StringBuilder stringBuilder = new StringBuilder();
//            for (ConstraintViolation<ScheduledTasksLogVo> item : message) {
//                stringBuilder.append(item.getMessage()).append(",");
//            }
//            if (stringBuilder.length() > 0) {
//                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
//                return R.fail(stringBuilder.toString());
//            }
//        }
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        int count = scheduledTasksLogMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<ScheduledTasksLogVo> list = scheduledTasksLogMapper.findPageList(row);
        return R.ok(list, count);
    }

    /**
     * @param row 1 定时任务日志类
     * @return com.framework.common.model.response.R
     * @title 新增定时任务日志
     * @description 新增定时任务日志
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(ScheduledTasksLogVo row) {
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);


        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        scheduledTasksLogMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 定时任务日志类
     * @return com.framework.common.model.response.R
     * @title 编辑定时任务日志
     * @description 编辑定时任务日志
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(ScheduledTasksLogVo row) {
        SystemUserVo systemUserVo = getUser();
        ScheduledTasksLogVo queryParam = scheduledTasksLogMapper.selectByPrimaryKey(row.getId());
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        Long userId = systemUserVo.getId();
        Date date = new Date();


        row.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        scheduledTasksLogMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除定时任务日志
     * @description 删除定时任务日志
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        ScheduledTasksLogVo row = new ScheduledTasksLogVo();
        Date date = new Date();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        row.setId(id);


        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        scheduledTasksLogMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除定时任务日志
     * @description 批量删除定时任务日志
     * @author 龘鵺
     * @datetime 2025-04-25 09:06:26
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        ScheduledTasksLogVo row = new ScheduledTasksLogVo();
        SystemUserVo systemUserVo = getUser();
        Long userId = systemUserVo.getId();
        Date date = new Date();
        row.setIdList(idList);


        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        //scheduledTasksLogMapper.deleteList(row);
        return R.ok();
    }
}